package com.intellagric.service.traceability.Impl.trace_person;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.PersonDao;
import com.intellagric.mapper.TracePersonMapper;
import com.intellagric.module.traceability.PersonVo;
import com.intellagric.pojo.TracePerson;
import com.intellagric.pojo.TracePersonExample;
import com.intellagric.service.traceability.trace_person.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/11/7
 * @DESC： 人员信息管理
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private TracePersonMapper personMapper;

    @Autowired
    private PersonDao personDao;

    /**
     * 添加人员信息
     * @param person
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addPerson(TracePerson person) {
        if (personMapper.insertSelective(person) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 添加人员信息
     * @param
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deletePerson(String id) {
        if (personMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 添加人员信息
     * @param person
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editPerson(TracePerson person) {
        if (personMapper.updateByPrimaryKeySelective(person) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询人员信息
     * @param id
     * @return TracePerson
     */
    @Override
    public TracePerson getPersonById(String id) {
        return personMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询人员信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getPersonList() {
        TracePersonExample personExample = new TracePersonExample();
        personExample.createCriteria().andIdIsNotNull();
        List<TracePerson> personList = personMapper.selectByExample(personExample);
        return LayuiDataGridResult.success().add(personList,personList.size());
    }

    /**
     * 分页查询人员信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getPersonPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TracePersonExample personExample = new TracePersonExample();
        personExample.createCriteria().andIdIsNotNull();
        List<TracePerson> personList = personMapper.selectByExample(personExample);
        return LayuiDataGridResult.success().add(personList,this.getPersonList().getCount());
    }

    /**
     * 根据Vo查询人员信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getPersonByVo(int page, int limit,PersonVo personVo) {
        PageHelper.startPage(page,limit);
         List<TracePerson> personList = personDao.selectPersonByVo(personVo);
        PageInfo<TracePerson> pageInfo = new PageInfo<>(personList);
        return LayuiDataGridResult.success().add(personList,pageInfo.getTotal());
    }
}
